package com.szyjjk.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.szyjjk.entity.*;
import com.szyjjk.imageCrawler.CrawlerTool;
import com.szyjjk.service.*;
import com.szyjjk.util.DateUtil;
import com.szyjjk.util.ExportExcelUtils;
import com.szyjjk.util.FileUtil;
import com.szyjjk.util.HttpTool;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private RunSiteService runSiteService;

    @Autowired
    private UserService userService;

    @Autowired
    private RunObserve runObserve;

    @Autowired
    private SysImageCrawlerService sysImageCrawlerService;

    @Autowired
    private SysImageCrawlerFilesService sysImageCrawlerFilesService;

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 跳转到首页
     * @Date 14:16 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/home.do")
    public String index() {

        return "home/homeHead";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 基础数据查询
     * @Date 15:20 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/homebaseData.do")
    public String homebaseData() {

        return "home/homebaseData";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 基础数据英文
     * @Date 10:00 2020/10/19
     * @Param []
     **/
    @RequestMapping(value = "/homebaseDataEn.do")
    public String homebaseDataEn() {

        return "home/homebaseData_en";
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 接口硬件展示
     * @Date 15:22 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/hardWare.do")
    public String hardWare() {
        return "home/homeHardWare";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 系统监控
     * @Date 14:20 2020/4/29
     * @Param []
     **/
    @RequestMapping(value = "/homeMonitoring.do")
    public String homeMonitoring() {

        return "home/homeMonitoring";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 实时数据
     * @Date 15:20 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/homeRealTime.do")
    public String homeRealTime() {
        return "home/homeRealTime";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 预警
     * @Date 10:04 2020/9/29
     * @Param []
     **/
    @RequestMapping(value = "/homeWarning.do")
    public String homeWarning() {
        return "home/homeWarning";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 查看七天的弹窗
     * @Date 16:27 2020/7/13
     * @Param []
     **/
    @RequestMapping(value = "/svenDayDialog.do")
    public String svenDayDialog() {

        return "dialog/sevenDayDialog";
    }

    /**
     * @return java.util.List<java.util.Map>
     * @Author yangyue
     * @Description //TODO 返回所有区域数据
     * @Date 15:20 2020/3/26
     * @Param []
     **/
    @RequestMapping(value = "/areaDataJson.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Map> areaDataJson(String languageType) {
        List<Map> array = new ArrayList<>();
        List<RunArea> runAreaArray = runSiteService.findAllArea();
        for (RunArea runArea : runAreaArray) {
            Map<String, String> reuslt = new HashedMap();
            reuslt.put("value", runArea.getId());
            if ("en".equals(languageType)) {
                reuslt.put("label", FileUtil.ToPinyin(runArea.getAreaName()));
            } else {
                reuslt.put("label", runArea.getAreaName());
            }

            array.add(reuslt);
        }
        return array;
    }


    /**
     * @return java.util.List<java.util.Date>
     * @Author yangyue
     * @Description //TODO 返回七天日期
     * @Date 9:11 2020/7/14
     * @Param []
     **/
    @RequestMapping(value = "/returnSevenDay.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Date> returnSevenDay() {
        List<Date> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        result.add(calendar.getTime());
        for (int i = 0; i < 6; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            result.add(calendar.getTime());
        }
        return result;
    }


    /**
     * @return com.szyjjk.entity.RunSiteData
     * @Author yangyue
     * @Description //TODO 返回弹出数据
     * @Date 10:49 2020/7/14
     * @Param [number, type]
     **/
    @RequestMapping(value = "/returnMapSevenDayChart.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<String> returnMapSevenDayChart(String SiteID, Integer number, String type) {
        List<Date> daySU = returnSevenDay();
        List<String> result = new ArrayList<>();
        List<RunSiteData> siteData = this.runSiteService.findSiteDataChart(SiteID, type, daySU.get(number));
        if (siteData.size() > 0) {
            RunSiteData runSiteData = siteData.get(0);
            result.add(runSiteData.getDataH0());
            result.add(runSiteData.getDataH1());
            result.add(runSiteData.getDataH2());
            result.add(runSiteData.getDataH3());
            result.add(runSiteData.getDataH4());
            result.add(runSiteData.getDataH5());
            result.add(runSiteData.getDataH6());
            result.add(runSiteData.getDataH7());
            result.add(runSiteData.getDataH8());
            result.add(runSiteData.getDataH9());
            result.add(runSiteData.getDataH10());
            result.add(runSiteData.getDataH11());
            result.add(runSiteData.getDataH12());
            result.add(runSiteData.getDataH13());
            result.add(runSiteData.getDataH14());
            result.add(runSiteData.getDataH15());
            result.add(runSiteData.getDataH16());
            result.add(runSiteData.getDataH17());
            result.add(runSiteData.getDataH18());
            result.add(runSiteData.getDataH19());
            result.add(runSiteData.getDataH20());
            result.add(runSiteData.getDataH21());
            result.add(runSiteData.getDataH22());
            result.add(runSiteData.getDataH23());
            return result;
        }
        return null;
    }


    /**
     * @return java.util.List<java.util.Map>
     * @Author yangyue
     * @Description //TODO 根据区域ID 查询站点 --返回深圳站点数据
     * @Date 14:35 2020/4/7
     * @Param [areaId]
     **/
    @RequestMapping(value = "/siteByAreaIDJson.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Map> siteByAreaIDJson(String areaId, String languageType) {
        List<Map> array = new ArrayList<>();
        List<RunSite> runAreaArray = runSiteService.findSiteByAreaID(areaId);
        for (RunSite runsite : runAreaArray) {
            Map<String, String> reuslt = new HashedMap();
            reuslt.put("value", runsite.getId());
            if ("en".equals(languageType)) {
                //中文需要翻译
                reuslt.put("label", FileUtil.ToPinyin(runsite.getSiteName()));
            } else {
                reuslt.put("label", runsite.getSiteName());
            }

            array.add(reuslt);
        }
        return array;
    }


    /**
     * @return java.util.List<java.util.Map>
     * @Author yangyue
     * @Description //TODO 根据类别查询的站点--- 香港
     * @Date 14:37 2020/4/7
     * @Param [type]
     **/
    @RequestMapping(value = "/siteByTypeJson.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Map> siteByTypeJson(String type, String languageType) {
        List<Map> array = new ArrayList<>();
        List<RunSite> runAreaArray = runSiteService.findSiteByType(type);
        for (RunSite runsite : runAreaArray) {
            Map<String, String> reuslt = new HashedMap();
            if ("en".equals(languageType)) {
                //中文需要翻译
                reuslt.put("label", FileUtil.ToPinyin(runsite.getSiteName()));
            } else {
                reuslt.put("label", runsite.getSiteName());
            }
//            reuslt.put("label", runsite.getSiteName());
            reuslt.put("value", runsite.getId());
            array.add(reuslt);
        }
        return array;
    }


    /**
     * @return java.util.List<java.util.Map>
     * @Author yangyue
     * @Description //TODO 返回站点的信息
     * @Date 14:35 2020/4/7
     * @Param [type]
     **/
    @RequestMapping(value = "/siteLonAndLat.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Map> siteLonAndLat(String type, String languageType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Map> resultReturn = new ArrayList<>();
        List<RunSite> rsultArray = runSiteService.findSiteByType(type);
        for (int i = 0; i < rsultArray.size(); i++) {
            Map<String, Map<String, String>> mapT = new HashedMap();
            Map mapG = new HashedMap();
            mapG.put("x", Double.valueOf(rsultArray.get(i).getSiteLon()));
            mapG.put("y", Double.valueOf(rsultArray.get(i).getSiteLat()));
            mapG.put("type", "point");
            mapT.put("geometry", mapG);

            Map mapA = new HashedMap();     //A  Area 区域
            mapA.put("ObjectID", i);
            if ("en".equals(languageType)) {
                mapA.put("siteName", FileUtil.ToPinyin(rsultArray.get(i).getSiteName()));
            } else {
                mapA.put("siteName", rsultArray.get(i).getSiteName());
            }


            if ("深圳气象数据网".equals(type)) {
                //深圳气象网
                //香港
                //在根据 站点ID ，然后不同的类别 时间排序
                List<RunSiteData> shiduList = runSiteService.findSiteDataMap(rsultArray.get(i).getId(), "相对湿度");
                List<RunSiteData> qiwenList = runSiteService.findSiteDataMap(rsultArray.get(i).getId(), "气温");
                List<RunSiteData> qiyaList = runSiteService.findSiteDataMap(rsultArray.get(i).getId(), "气压");
                List<RunSiteData> jiangshuiList = runSiteService.findSiteDataMap(rsultArray.get(i).getId(), "降水");
                List<RunSiteData> fengxiangiList = runSiteService.findSiteDataMap(rsultArray.get(i).getId(), "风向");
                mapA.put("time", simpleDateFormat.format(qiwenList.get(0).getDataTime()));
                try {
                    //湿度
                    if (shiduList.size() > 0) {
                        String[] shidu = getDisposeData(shiduList.get(0));
                        mapA.put("shidu", shidu[1] + "（" + (Integer.parseInt(shidu[0]) - 1) + ":00)");
                    }
                    //气温
                    if (qiwenList.size() > 0) {
                        String[] qiwen = getDisposeData(qiwenList.get(0));
                        mapA.put("qiwen", qiwen[1] + "（" + (Integer.parseInt(qiwen[0]) - 1) + ":00)");
                    }

                    //气压
                    if (qiyaList.size() > 0) {
                        String[] qiya = getDisposeData(qiyaList.get(0));
                        mapA.put("qiya", qiya[1] + "（" + (Integer.parseInt(qiya[0]) - 1) + ":00)");
                    }

                    //降水
                    String[] jiangshui = getDisposeData(jiangshuiList.get(0));
                    if (jiangshuiList.size() > 0 && jiangshui[0] == null) {

                        mapA.put("jiangshui", jiangshui[1] + "（" + (Integer.parseInt(jiangshui[0]) - 1) + ":00)");
                    }
                    //风向
                    if (fengxiangiList.size() > 0) {
                        String[] fengxiang = getDisposeData(fengxiangiList.get(0));
                        mapA.put("fengxiang", fengxiang[1] + "（" + (Integer.parseInt(fengxiang[0]) - 1) + ":00)");
                    }
                    mapA.put("siteID", rsultArray.get(i).getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (type.equals("香港天文台")) {
                List<RunSiteHKObs> HongKongArray = runSiteService.findsiteHongKongBySiteID(rsultArray.get(i).getId());
                if (HongKongArray.size() > 0) {
                    RunSiteHKObs runSiteHKObs = HongKongArray.get(0);
                    mapA.put("time", simpleDateFormat.format(runSiteHKObs.getObsTime()));    //时间
                    mapA.put("qiya", runSiteHKObs.getObsQiya());//气压
                    mapA.put("qiwenMax", runSiteHKObs.getObsQiwenMax().replace("#", "") + "℃");
                    mapA.put("qiwenPj", runSiteHKObs.getObsQiwenPj().replace("#", "") + "℃");
                    mapA.put("qiwenMin ", runSiteHKObs.getObsQiwenMin().replace("#", "") + "℃");
                    mapA.put("ludian", runSiteHKObs.getObsLudian().replace("#", "") + "℃");
                    mapA.put("shidu", runSiteHKObs.getObsShidu());
                    mapA.put("yunliang", runSiteHKObs.getObsYunliang());
                    mapA.put("yuliang", runSiteHKObs.getObsYuliang());
                    mapA.put("fengxiang", runSiteHKObs.getObsFengxiang());
                    mapA.put("fengshu", runSiteHKObs.getObsFengsu());
                } else {
                    System.out.println(rsultArray.get(i).getSiteName() + "找不数据");
                }

            }
            mapT.put("attributes", mapA);

            resultReturn.add(mapT);
        }
        return resultReturn;
    }

    /**
     * @return java.util.Map<java.lang.String   ,   java.lang.String>
     * @Author yangyue
     * @Description //TODO  返回对应的站点经纬度
     * @Date 14:04 2020/7/13
     * @Param [id]
     **/
    @RequestMapping(value = "/getSiteXY.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, String> getSiteXY(String id) {
        RunSite site = runSiteService.findbyIdSite(id);
        Map<String, String> map = new HashedMap();
        map.put("X", site.getSiteLon());
        map.put("Y", site.getSiteLat());
        return map;
    }


    /**
     * @return java.lang.String[]
     * @Author yangyue
     * @Description //TODO 返回站点数据最新的数据 深圳气象
     * @Date 14:45 2020/3/27
     * @Param [data]
     **/
    public String[] getDisposeData(RunSiteData data) throws IllegalAccessException {
        Class cls = data.getClass();
        Field[] fields = cls.getDeclaredFields();
        String values = "";
        String hours = "";
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            f.setAccessible(true);
            if (f.getName().contains("dataH")) {
                if (f.get(data) != null && !f.get(data).equals("")) {
                    values = f.get(data) + "";
                    hours = f.getName().replace("dataH", "");
                }
            }
        }
        return new String[]{hours, values};
    }

    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteHKObs>
     * @Author yangyue
     * @Description //TODO 分页查询历史数据
     * @Date 10:15 2020/4/14
     * @Param [siteID, pageNo, pageSize]
     **/
    @RequestMapping(value = "/getPageSiteHkObs.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject getPageSiteHkObs(HttpServletRequest request, String siteID, int pageNo, int pageSize, String beginTime, String endTime, String siteType) {
        JSONObject jsonObject = new JSONObject();
        Date[] timeArray = DateUtil.StringToDate5(beginTime, endTime);
        Date beginDate = timeArray[0];
        Date endDate = timeArray[1];
        try {
            if (siteType.equals("香港天文台")) {
                //查询香港天文台数据
                List<RunSiteHKObs> runSiteHKObsPageInfo = this.runSiteService.findPageSiteHKObs((pageNo - 1) * pageSize, pageSize, siteID, beginDate, endDate);
                for (RunSiteHKObs runSiteHKObs : runSiteHKObsPageInfo) {
                    //更换时间
                    RunSite site = this.runSiteService.findbyIdSite(runSiteHKObs.getSiteId());
                    runSiteHKObs.setSiteId(site.getSiteName());
                }
                jsonObject.put("list", runSiteHKObsPageInfo);
                jsonObject.put("count", runSiteService.findPageSiteHKObsbyCount(siteID, beginDate, endDate));
            } else if (siteType.equals("潮汐数据")) {
                List<RunTide> runTidesList = this.runObserve.findPageRunTide((pageNo - 1) * pageSize, pageSize, siteID, beginDate, endDate);
                for (RunTide run : runTidesList) {
                    //更换时间
                    RunSite site = this.runSiteService.findbyIdSite(run.getSideId());
                    run.setSideId(site.getSiteName());
                }
                jsonObject.put("list", runTidesList);
                jsonObject.put("count", runObserve.findPageRunTideCount(siteID, beginDate, endDate));
            } else if (siteType.equals("水质数据")) {
                List<RunWater> runWaterList = this.runObserve.findPageWater((pageNo - 1) * pageSize, pageSize, siteID, beginDate, endDate);
                for (RunWater run : runWaterList) {
                    //更换时间
                    RunSite site = this.runSiteService.findbyIdSite(run.getSiteId());
                    run.setSiteId(site.getSiteName());
                }
                jsonObject.put("list", runWaterList);
                jsonObject.put("count", runObserve.findPageRunTideCount(siteID, beginDate, endDate));
            } else if (siteType.equals("深圳气象数据网")) {
                List<RunSiteData> runSiteData = this.runSiteService.findsitrDatePage((pageNo - 1) * pageSize, pageSize, siteID, beginDate, endDate);
                for (RunSiteData run : runSiteData) {
                    //更换时间
                    RunSite site = this.runSiteService.findbyIdSite(run.getSiteId());
                    run.setSiteId(site.getSiteName());
                }
                jsonObject.put("list", runSiteData);
                jsonObject.put("count", runSiteService.findsitrDatePageCount(siteID, beginDate, endDate));
            } else if (siteType.equals("太阳辐射")) {
                List<RunSun> runSuns = this.runObserve.findSunPage((pageNo - 1) * pageSize, pageSize, siteID, beginDate, endDate);
                List<String> imgUrl = new ArrayList<>();
                for (RunSun runSun : runSuns) {
                    RunSite site = this.runSiteService.findbyIdSite(runSun.getSiteId());
                    runSun.setSiteId(site.getSiteName());
                    int indexs = runSun.getSunDirectPath().lastIndexOf("\\");
                    String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort();
                    //通过截取之后在来算路径

                    runSun.setSunGrossPath(path + "/home/readPartImage.do?address=" + runSun.getSunGrossPath().substring(indexs + 1) + "&FilePath=" + java.net.URLEncoder.encode(runSun.getSunGrossPath().substring(0, indexs + 1), "GBK"));
                    imgUrl.add(runSun.getSunGrossPath());
                    runSun.setSunDirectPath(path + "/home/readPartImage.do?address=" + runSun.getSunDirectPath().substring(indexs + 1) + "&FilePath=" + java.net.URLEncoder.encode(runSun.getSunDirectPath().substring(0, indexs + 1), "GBK"));
                    imgUrl.add(runSun.getSunDirectPath());
                }
                jsonObject.put("list", runSuns);
                jsonObject.put("count", runObserve.findSunPageCount(siteID, beginDate, endDate));
                jsonObject.put("imgUrl", imgUrl);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    //根据类别查询观测数据  --观测数据主要分为三类 太阳辐射  水质数据  潮汐数据
    @RequestMapping(value = "/getObsType.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Map> getObsType(String type, String languageType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        List<Map> result = new ArrayList<>();
        List<RunSite> runSites = this.runSiteService.findSiteByType(type);
        if (type.equals("水质数据")) {
            int i = 0;
            for (RunSite runSite : runSites) {
                i++;
                Map<String, Map<String, String>> mapT = new HashedMap();        //T
                Map mapG = new HashedMap();
                mapG.put("x", Double.valueOf(runSite.getSiteLon()));
                mapG.put("y", Double.valueOf(runSite.getSiteLat()));
                mapG.put("type", "point");
                mapT.put("geometry", mapG);

                Map mapA = new HashedMap();     //A  Area 区域
                mapA.put("ObjectID", i);
                String junluo = "(菌落数/100毫升)";
                String rongjieyang = "(溶解氧 (毫克/升))";
                if ("en".equals(languageType)) {
                    mapA.put("siteName", FileUtil.ToPinyin(runSite.getSiteName()));
                    junluo = "Dissolved oxygen (mg/L)";
                    rongjieyang = "E.coli(Colony count /100 ml)";
                } else {
                    mapA.put("siteName", runSite.getSiteName());
                }


                List<RunWater> runTideList = this.runObserve.findWaterBySiteID(runSite.getId());
                if (runTideList.size() > 0) {
                    mapA.put("time", HttpTool.monthSimpDate.format(runTideList.get(0).getWaterTime()));
                    mapA.put("germ", runTideList.get(0).getWaterOxygen() + junluo);//	大肠桿菌 (菌落数/100毫升)
                    mapA.put("oxygen", runTideList.get(0).getWaterGerm() + rongjieyang);//	溶解氧 (毫克/升)
                    mapT.put("attributes", mapA);
                }
                result.add(mapT);
            }
        } else if (type.equals("潮汐数据")) {

            int i = 0;
            for (RunSite runSite : runSites) {
                i++;
                //开始查询潮汐数据 然后主要返回两天数据，今天的和明天的
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DAY_OF_MONTH, 1);

                Map<String, Map<String, String>> mapT = new HashedMap();        //T
                Map mapG = new HashedMap();
                mapG.put("x", Double.valueOf(runSite.getSiteLon()));
                mapG.put("y", Double.valueOf(runSite.getSiteLat()));
                mapG.put("type", "point");
                mapT.put("geometry", mapG);

                Map mapA = new HashedMap();     //A  Area 区域
                mapA.put("ObjectID", i);
                if ("en".equals(languageType)) {
                    mapA.put("siteName", FileUtil.ToPinyin(runSite.getSiteName()));
                } else {
                    mapA.put("siteName", runSite.getSiteName());
                }


                Date tomorrow = HttpTool.simpToData(calendar.getTime());
                List<RunTide> runTideList = this.runObserve.findRunTideBySidAndDate(runSite.getId(), tomorrow);
                if (runTideList.size() == 1) {
                    mapA.put("time", HttpTool.shotSimpDate.format(tomorrow));
                    if (runTideList.get(0).getTideTime1() != null) {
                        mapA.put("time1", simpleDateFormat.format(runTideList.get(0).getTideTime1()));
                    }
                    if (runTideList.get(0).getTideTime2() != null) {
                        mapA.put("time2", simpleDateFormat.format(runTideList.get(0).getTideTime2()));
                    }
                    if (runTideList.get(0).getTideTime3() != null) {
                        mapA.put("time3", simpleDateFormat.format(runTideList.get(0).getTideTime3()));
                    }
                    if (runTideList.get(0).getTideTime4() != null) {
                        mapA.put("time4", simpleDateFormat.format(runTideList.get(0).getTideTime4()));
                    }

                    mapA.put("hight1", runTideList.get(0).getTideHight1());
                    mapA.put("hight2", runTideList.get(0).getTideHight2());
                    mapA.put("hight3", runTideList.get(0).getTideHight3());
                    mapA.put("hight4", runTideList.get(0).getTideHight4());
                    mapA.put("siteID", runSite.getId());

                    mapT.put("attributes", mapA);
                } else {
                    System.out.println("不存在或者多::" + runSite.getSiteName());
                }
                result.add(mapT);

            }
        } else if (type.equals("太阳辐射")) {

            int i = 0;
            for (RunSite runSite : runSites) {
                i++;
                Map<String, Map<String, String>> mapT = new HashedMap();        //T
                Map mapG = new HashedMap();
                mapG.put("x", Double.valueOf(runSite.getSiteLon()));
                mapG.put("y", Double.valueOf(runSite.getSiteLat()));
                mapG.put("type", "point");
                mapT.put("geometry", mapG);

                Map mapA = new HashedMap();
                mapA.put("ObjectID", i);

//                mapA.put("siteName", runSite.getSiteName());
                if ("en".equals(languageType)) {
                    mapA.put("siteName", FileUtil.ToPinyin(runSite.getSiteName()));
                } else {
                    mapA.put("siteName", runSite.getSiteName());
                }


                List<RunSun> runSunList = this.runObserve.findSunBySiteID(runSite.getId());
                if (runSunList.size() > 0) {
                    //需要转化为Base 64
                    mapA.put("time", HttpTool.shotSimpDate.format(runSunList.get(0).getSunTime()));
                    int indexs = runSunList.get(0).getSunDirectPath().lastIndexOf("\\");
                    try {
                        mapA.put("direct", runSunList.get(0).getSunGrossPath().substring(indexs + 1) + "&FilePath=" + java.net.URLEncoder.encode(runSunList.get(0).getSunGrossPath().substring(0, indexs + 1), "GBK"));
                        mapA.put("gross", runSunList.get(0).getSunDirectPath().substring(indexs + 1) + "&FilePath=" + java.net.URLEncoder.encode(runSunList.get(0).getSunDirectPath().substring(0, indexs + 1), "GBK"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    mapT.put("attributes", mapA);
                }
                result.add(mapT);
            }
        }

        return result;
    }


    //获得太阳辐射的图片
    @RequestMapping("/readPartImage.do")
    @ResponseBody
    public void readPartImage(HttpServletRequest request, HttpServletResponse response, String address, String FilePath) throws Exception {
        File file = new File(FilePath + address);
        if (!file.exists()) {
            System.out.println(FilePath + address + "文件不存在");
            return;
        }
        InputStream is = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream os = response.getOutputStream();
        int ch = 0;
        while (-1 != (ch = is.read())) {
            baos.write(ch);
        }
        os.write(baos.toByteArray());
        is.close();
        baos.close();
        os.close();
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSite>
     * @Author yangyue
     * @Description //TODO 返回站点信息
     * @Date 10:43 2020/4/9
     * @Param [type]
     **/
    @RequestMapping(value = "/getObsSite.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<RunSite> getObsSite(String type, String languageType) {
        List<RunSite> runSites = this.runSiteService.findSiteByType(type);
        for (RunSite runSite : runSites) {
            if ("en".equals(languageType)) {
                runSite.setSiteName(FileUtil.ToPinyin(runSite.getSiteName()));
            }
        }
        return runSites;
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 成果数据展示
     * @Date 15:22 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/homeResultData.do")
    public String homeResultData() {

        return "home/homeResultData";
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 系统设置
     * @Date 15:22 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/homeSystem.do")
    public String homeSystem() {
        return "home/homeSystem";
    }

    /**
     * 登入校验
     *
     * @param userBo
     * @return
     */
    @RequestMapping(value = "/checkLogin.do")
    @ResponseBody
    public ResponseBo delUser(UserBo userBo) {
        ResponseBo rb = new ResponseBo();
        if (StringUtils.isBlank(userBo.getUserName()) || StringUtils.isBlank(userBo.getPassWord())) {
            rb.setCode(0);
            rb.setMessage("用户名或密码为空");
            return rb;
        }
        UserBo user = userService.queryLogin(userBo);
        if (user == null) {
            rb.setCode(0);
            rb.setMessage("用户名或密码不正确");
        }
        return rb;
    }


    /**
     * @return java.util.List<com.szyjjk.entity.SysImageCrawler>
     * @Author yangyue
     * @Description //TODO 返回所有爬虫
     * @Date 14:13 2020/5/8
     * @Param []
     **/
    @RequestMapping(value = "/getAllImageCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<SysImageCrawler> getAllImageCrawler(String languageType) {
        List<SysImageCrawler> runSites = sysImageCrawlerService.findCrewlerAll();
        for (SysImageCrawler runSite : runSites) {
            if (languageType.equals("zh")) {
                if (runSite.getSatellite().contains("MOD")) {
                    runSite.setSatellite("MODIS遥感影像");
                } else if (runSite.getSatellite().contains("Himiwari")) {
                    runSite.setSatellite("HIMAWARI-8卫星影像");
                } else if (runSite.getSatellite().contains("Aviso")) {
                    runSite.setSatellite("AVISO卫星影像");
                }
            } else if (languageType.equals("en")) {
                if (runSite.getSatellite().contains("MOD")) {
                    runSite.setSatellite("MODIS Remote sensing image");
                } else if (runSite.getSatellite().contains("Himiwari")) {
                    runSite.setSatellite("HIMAWARI-8 Satellite image");
                } else if (runSite.getSatellite().contains("Aviso")) {
                    runSite.setSatellite("AVISO Satellite image");
                }
            }

        }
        return runSites;
    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 返回影像
     * @Date 10:34 2020/6/19
     * @Param [pageNo, pageSize, id, begDate, endDate]
     **/
    @RequestMapping(value = "/getImageCrawlerQuery.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject getImageCrawlerQuery(int pageNo, int pageSize, String id, String begDate, String endDate, String languageType) {
        JSONObject jsonObject = new JSONObject();
        if (!StringUtils.isNotEmpty(id)) {
            //没有就返回为空了
            return null;
        }

        Date[] timeArray = DateUtil.StringToDate5(begDate, endDate);
        Date begintime = timeArray[0];
        Date endtime = timeArray[1];

        String fenbl = "分辨率";
        String imgeTitle = "影像文件四个顶点的经纬度坐标为：";
        String Satellite = "卫星";
        if ("en".equals(languageType)) {
            fenbl = "Resolution";
            imgeTitle = "The latitude and longitude coordinates of the four vertices of the image file are:";
            Satellite = "Satellite";
        }

        List<SysImageCrawlerFiles> runSites = sysImageCrawlerFilesService.getPageCrawlerFileByTme((pageNo - 1) * pageSize, 10, id, begintime, endtime);
        for (SysImageCrawlerFiles runSite : runSites) {
            String lonAndLat = runSite.getFileLonglat();
            if (StringUtils.isNotEmpty(lonAndLat)) {
                String result = "";
                String[] spileString = lonAndLat.split(";");
                for (String str : spileString) {
                    String[] head = str.split(",");
                    String lon = head[0].substring(0, 6);
                    String lat = head[1].substring(0, 5);
                    String lonlat = lon + "°E" + lat + "°N";
                    result = result + lonlat + "\n";
                }
                runSite.setFileLonglat(imgeTitle + "：\n" + result);
            }
            runSite.setFileSize(FileUtil.getPrintSize(Long.parseLong(runSite.getFileSize())));
            //开始进行中文说明

            if (runSite.getFileName().contains("MOD021KM")) {
                runSite.setFileType(Satellite + ":Terra \n " + fenbl + "：21KM");
            } else if (runSite.getFileType().contains("MYD021KM")) {
                runSite.setFileType(Satellite + ":Aqua \n " + fenbl + "：21KM");
            }

        }
        jsonObject.put("list", runSites);
        jsonObject.put("count", sysImageCrawlerFilesService.findPageImageCount(id, begintime, endtime));
        return jsonObject;
    }


    @RequestMapping(value = "/undefined", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String undefined() {
        return "?????";
    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 下载遥感影像 --查询是否存在
     * @Date 15:58 2020/6/15
     * @Param [ID]
     **/
    @RequestMapping(value = "/downLoadModisFile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject downLoadModisFile(String ID, HttpServletResponse response) {
        JSONObject jsonObject = new JSONObject();
        //首先找到下载信息
        SysImageCrawlerFiles sysImageCrawlerFilesInfo = this.sysImageCrawlerFilesService.findByIdCrawlerFiles(ID);
        SysImageCrawler sysImageCrawler = this.sysImageCrawlerService.findByIdCrawler(sysImageCrawlerFilesInfo.getCrawlerDownId());
        File file = new File(sysImageCrawler.getSavePath() + sysImageCrawlerFilesInfo.getFileName());
        if (!file.exists()) {
            jsonObject.put("code", 400);
            return jsonObject;
        }
        jsonObject.put("code", 200);

        jsonObject.put("path", sysImageCrawler.getSavePath());
        jsonObject.put("fileName", sysImageCrawlerFilesInfo.getFileName());

        return jsonObject;
    }

    /**
     * @return void
     * @Author yangyue
     * @Description //TODO 下载文件
     * @Date 14:57 2020/6/16
     * @Param [savePath, fileName, response]
     **/
    @RequestMapping(value = "/downLoadWindowLoadfile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public void downLoadWindowLoadfile(String savePath, String fileName, HttpServletResponse response) {
        FileUtil.downloadFiles(response, savePath, fileName);
    }

    //下载历史数据


    @RequestMapping(value = "/downLoadAllHistroy", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject downLoadAllHistroy(HttpServletRequest request, HttpServletResponse response, String siteID, String beginTime, String endTime, String siteType) {
        JSONObject resultJsonObjet = new JSONObject();
        //首先查询数据
        JSONObject rueslt = getPageSiteHkObs(request, siteID, 1, 999999999, beginTime, endTime, siteType);
//        if(siteType.equals("太阳辐射")){
//
//        }else{
//
//        }

        ExportExcelUtils ex = ExportExcelUtils.returnHistory(siteType, rueslt, response, request);

        ex.exportData();
        resultJsonObjet.put("flag", "true");
        return resultJsonObjet;
    }









//    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ResponseBody
//    public JSONObject test(HttpServletRequest request) {
//        JSONObject resultJsonObjet = new JSONObject();
//        String sb = request.getContextPath();
//        resultJsonObjet.put("a", sb);
//        String as = request.getContextPath();
//        return resultJsonObjet;
//    }

}
